package com.hwua.dao.Impl;

import com.hwua.Util.C3P0Util;
import com.hwua.dao.ICommentDao;
import com.hwua.entity.Comment;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.util.List;

public class CommentDaoImpl implements ICommentDao {
    private QueryRunner queryRunner=null;

    public CommentDaoImpl() {
        queryRunner=new QueryRunner(C3P0Util.getCpds());
    }

    @Override
    public List<Comment> queryForLimit(Integer start,Integer pageSize) throws Exception {
        String sql="select * from amz_comment ORDER BY create_time DESC limit ?,?";
        Object[] args={start,pageSize};
        return queryRunner.query(sql,new BeanListHandler<Comment>(Comment.class),args);
    }

    @Override
    public Long queryCount() throws Exception {
        String sql="select count(*) from amz_comment";
        return queryRunner.query(sql,new ScalarHandler<>());
    }

    @Override
    public int insertComment(Comment comment) throws Exception {
        String sql="insert into amz_comment values(null,?,?,?,?,?,null)";
        Object[] args={comment.getReply(),comment.getContent(),comment.getCreate_time(),comment.getReply_time(),comment.getNick_name()};
        return queryRunner.update(sql,args);
    }

    @Override
    public List<Comment> queryAll() throws Exception {
        String sql="select * from amz_comment";
        return queryRunner.query(sql,new BeanListHandler<Comment>(Comment.class));
    }
}
